package net.tooeye.easymes.activities;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import net.tooeye.easymes.common.BaseActivity;
import net.tooeye.easyplus.R;

public class WebAppActivity extends BaseActivity {

    WebView mWebView;
    String url = "/AppIndex/Index";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_app);
        ActionBar bar = getSupportActionBar();
        if (bar != null) {
            bar.hide();
        }
        try {
            Intent intent = getIntent();
            url = intent.getStringExtra("url");
        } catch (Exception e) {

        }

        mWebView = findViewById(R.id.webContent);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                loadurlLocalMethod(view, url);
                return false;
            }
        });
        mWebView.loadUrl("http://" + GetHost() + url + "&token=" + GetToken());

        Log.d("song", "onCreate: " + mWebView.getUrl());
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&mWebView.canGoBack()){
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    public void loadurlLocalMethod(final WebView webView, final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl(url);
            }
        });
    }

}
